package com.xing.server.execption;

import com.xing.server.base.R;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import java.sql.SQLNonTransientException;

/**
 * 全局异常处理
 *
 * @author: xingfuhao
 * @create: 2021-12-21 03:10
 */
//全局异常处理的注解
@RestControllerAdvice
public class GlobalException {

    @ExceptionHandler(SQLException.class)
    public R mySqlException(SQLException e){
        if(e instanceof SQLIntegrityConstraintViolationException){
            return R.error("该数据有关联数据，操作失败!");
        }
        return R.error("数据库异常，操作失败");
    }
}
